package com.eazy.lkv2.core.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.eazy.lkv2.web.controller.CrudController;
import com.eazy.lkv2.web.entity.MethodLog;
 
/**
 * 看名字就知道这个类是做什么得了吧。。。
 * @author apple
 */
@Component
@Aspect  
public class ServiceAop extends CrudController<MethodLog, Integer> {  
  
	// 定义参考 http://www.cnblogs.com/yanjunwu/p/3996570.html
    @Pointcut("execution(* com.eazy.lkv2.web.service..*(..))")  
    public void makeOneCake() {  
    }  
  
    @Before("makeOneCake()")
    public void before(JoinPoint joinPoint){
    }  
    
    @After("makeOneCake()")
    public void after(JoinPoint joinPoint){  
    }  
  
  	@AfterReturning("makeOneCake()")
  	public void afterReturn(JoinPoint joinPoint){
  	}
  	
  	@AfterThrowing(pointcut="makeOneCake()", throwing="ex")
  	public void afterThrow(JoinPoint joinPoint, Exception ex){
  	}
  	
    @Around(value = "makeOneCake()")  
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {  
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        long startTime = System.currentTimeMillis();  
        Object object = joinPoint.proceed();  
        long endTime = System.currentTimeMillis();  
        
        MethodLog method = new MethodLog();
        method.setClss(targetName);
        method.setMethod(methodName);
        method.setTime((endTime - startTime) + "");
        create(method);
        return object;	
    }  
  
}  